package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.GroupPushMsgDao;
import com.hangzhou.xyj.manager.NotifyManager;
import com.hangzhou.xyj.model.GroupPushMsgModel;
import com.hangzhou.xyj.model.type.EPushMessageType;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.MyOfficalMessageAdapter;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.ui.widget.CommonTitleBarView;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yaohuix on 2016/6/7  18:49
 * <p/>
 * 说明：
 */
public class MyOfficalMessageActivity extends BaseActivity {
    RecyclerView mRecyclerView;
    private MyOfficalMessageAdapter mAdapter;
    private List<GroupPushMsgModel> mAllMessage = new ArrayList<>();
    private GroupPushMsgDao mDao;
    private GroupPushMsgModel mMsgModel;
    private int pushType;

    @Override
    public void initIntentData(Intent intent) {
        mMsgModel = (GroupPushMsgModel) intent.getSerializableExtra(DataKey.OBJ);
        pushType = intent.getIntExtra(DataKey.PUSH_TYPE, -1);
    }

    @Override
    public void initDatas() {
        mDao = new GroupPushMsgDao(mContext);
        if (mMsgModel == null) {//登录以后的从数据库读取
            mAllMessage = mDao.getAllMessage("all");
            mDao.updateIsRead("all");
        } else {
            mAllMessage.add(mMsgModel);//未登录的情况
        }
        if (mAllMessage == null || mAllMessage.isEmpty()) {
            return;
        }
        mRecyclerView.setAdapter(mAdapter = new MyOfficalMessageAdapter(mContext, mAllMessage));
        if (mMsgModel != null) {
            mAdapter.setFromNotification(true);
        }
        if (pushType != -1) {
            NotifyManager.getInstance().cancelMsg(mContext, EPushMessageType.valueOf(pushType));//清除通知
            //重置计数
            switch (EPushMessageType.valueOf(pushType)) {
                case SINGLE_CHAT:
                    NotifyManager.mSingleChatCount = 0;//重置
                    break;
                case GROUP_CHAT:
                    NotifyManager.mGroupChatCount = 0;
                    break;
                case ADD_GROUP:
                    NotifyManager.mAddGroupCount = 0;
                    break;
                default:
                case SYSTEM:
                    NotifyManager.mSystemCount = 0;
                    break;
            }
        }
    }

    @Override
    public void initTop() {
        if (mMsgModel == null) {
            enableRightNav(true, "清空");
        }
        enableTitle(true, "戏友记官方");
        setOnNavRightListener(new CommonTitleBarView.OnNavRightListener() {
            @Override
            public void onNavRight() {//点击了 “清空”
                new TipsDialog(mContext, "提示", "确定要清空所有系统消息？", "确定", "取消") {
                    @Override
                    protected void OnClickNegativeButton() {
                        this.dismiss();
                    }

                    @Override
                    protected boolean OnClickPositiveButton() {
                        mDao.delete("all");
                        mAdapter.update(null);
                        return true;
                    }
                }.show();
            }
        });
    }

    @Override
    public void initLayouts() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_common_recycleview, null, false);
        view.setBackgroundColor(Color.parseColor("#F2F2F2"));
        setContentView(view);
    }

    @Override
    public void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_common);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void initListeners() {

    }
}
